#include <iostream>
#include <iomanip>

#include "String.h"
#include <cstring>
using std::strcmp;
using std::strcpy;
using std::strcat;

using std::setw;
String::String(const char*s):length((s!=0)?strlen(s):0)
{
	setString(s);
}
String::String(const String &copy):length(copy.length)
{
	std::cout<<"copy construcotr "<<copy.sPtr<<"\n";
	setString(copy.sPtr);
}
String::~String()
{
	std::cout<<"Destructor "<<(*this).sPtr<<"\n";
	delete [] sPtr;
}
const String &String::operator =(const String &right)
{
	std::cout<<"operator = apelat\n";
	if(&right!=this)
	{
		delete [] sPtr;
		(*this).length=right.length;
		setString(right.sPtr);
	}
	else
		std::cout<<"Incercare de asignare cu acelasi obiect\n";
	return *this;
}
const String &String::operator +=(const String &right)
{
	size_t newLength=(*this).length+right.length;
	char *temp=new char[newLength+1];
	strcpy(temp,(*this).sPtr);
	strcpy(temp+(*this).length,right.sPtr);
	delete [] sPtr;
	(*this).sPtr=temp;
	(*this).length=newLength;
	return *this;
}
bool String::operator!()const
{
	return (*this).length==0;
}
bool String::operator==(const String &right)const
{
	return strcmp((*this).sPtr,right.sPtr)==0;
}
bool String::operator<(const String &right)const
{
	return strcmp((*this).sPtr,right.sPtr)<0;
}
char &String::operator[](int sub)
{	
	if(sub<0||sub>=length)
	{
		std::cerr<<"eroare";
		
	}
	return (*this).sPtr[sub];
}
char String::operator[](int sub)const
{
	if(sub<0||sub>=(*this).length)
	{
		std::cerr<<"eroare";
	}
	return (*this).sPtr[sub];
}
String String::operator()(int index,int subLen)const
{
	if(index<0||index>=(*this).length||subLen<0)
	{
		std::cerr<<"eroare";
	}
	int len;
	if((subLen==0)||(index+subLen)>(*this).length)
		len=(*this).length-index;
	else
		len=subLen;
	char *temp=new char[len+1];
	strncpy(temp,&(*this).sPtr[index],len);
	temp[len]='\n';
	String tempString(temp);
	delete [] temp;
	return tempString;
}
int String::getLength()const
{
	return (*this).length;
}
void String::setString(const char *str2)
{
	(*this).sPtr=new char[(*this).length+1];
	if(str2!=0)
		strcpy((*this).sPtr,str2);
	else
		(*this).sPtr[0]='\0';
}
std::ostream &operator<<(std::ostream &out,const String &obj)
{
	out<<obj.sPtr;
	return out;
}
std::istream &operator >>(std::istream &inp,String &obj)
{
	char temp[100];
	inp>>setw(100)>>temp;
	obj=temp;
	return inp;
}
bool String::operator>(const String& right)const{
		return right<*this;
}
bool String::operator!=(const String& right)const{
		return !(*this==right);
}
bool String::operator <=(const String& right)const{
		return !(right<*this);		
}
bool String::operator >=(const String& right)const{
		return !(*this<right);
}
bool String::remove(int index,int subLen){
	if(index<0||index>=(*this).length||subLen<0)
	{
		std::cerr<<"eroare";
	}
	int x=subLen,v=0;
	char *p=new char[(*this).length+1];
	strcpy(p,(*this).sPtr);
	for(int i=0;i<=x;)
			{
				if(i==index-1 && i<=subLen)
					{
						i++;
					}
				else
					p[v++]=(*this).sPtr[i++];
			}
	(*this).sPtr=new char[(*this).length-subLen];
	strcpy(sPtr,p);
}
void String::cauta(char ch){

	for(int i=0;i<(*this).length;i++)
		{
				if(ch==(*this).sPtr[i])
					{
						std::cout<<"El exista";
						break;
					}
		}

}
